# -*- coding: utf-8 -*-
# @Author  : CaoHan
# @Time    : 2024/2/9 10:20

import matplotlib.pyplot as plt
import numpy as np
plt.rc("font",family='STHeiti')
# 设置全局字体大小
plt.rcParams.update({'font.size': 16})  # 设置全局默认字体大小


def normalDistribution(sensorNum):
    mu = 150  # 均值
    sigma = 40  # 标准差
    bin = 30
    reqNum = []
    arrivalTime = None
    while sum(reqNum) != sensorNum:
        reqNum = []
        # 生成正态分布随机数
        samples_normal = np.random.normal(mu, sigma, 10000)
        # 绘制直方图: 箱子数 概率密度 透明度
        hist_values, bin_edges, _ = plt.hist(samples_normal, bins=bin, density=True, alpha=0.5,
                                             label='Generated Samples')
        # 计算每个箱子的长度
        bin_widths = np.diff(bin_edges)  # 计算每个箱子的宽度
        arrivalTime = bin_edges

        for i in range(bin):
            reqNum.append(round(hist_values[i] * bin_widths[i] * sensorNum))  # 概率×数量,再取整
        plt.show()
    ans = []
    for i in range(bin):
        while reqNum[i] > 0:
            ans.append(arrivalTime[i])
            reqNum[i] -= 1
    return ans


def plotArrival(arrival_times):
    # 设置时间区间宽度(s)
    bin_width = 5
    # 确定时间区间
    bins = np.arange(0, max(arrival_times) + bin_width, bin_width)
    # 统计每个区间内的到达数量
    arrival_count, edges = np.histogram(arrival_times, bins=bins)
    # 计算区间中心点
    bin_centers = (edges[:-1] + edges[1:]) / 2
    # 累积到达数量
    cumulative_count = np.cumsum(arrival_count)
    # 绘制图像
    plt.figure(figsize=(10, 6))
    # 绘制累计到达数量折线图
    plt.plot(bin_centers, cumulative_count, marker='o', label="函数累计到达量", color='b')
    # 绘制每个区间的到达数量柱状图
    plt.bar(bin_centers, arrival_count, width=bin_width * 0.8, alpha=0.5, label="函数实时到达量（时间窗口=5s）",
            color='orange')
    # 添加图例、标签和标题
    plt.xlabel("时间/s")
    plt.ylabel("函数到达数量")
    plt.legend()
    plt.grid(True)
    # 显示图像
    plt.show()


def getFixArrival_150_50():
    return [21.307624831551436, 34.359564083546346, 47.411503335541255, 47.411503335541255, 47.411503335541255, 60.463442587536164, 60.463442587536164, 60.463442587536164, 60.463442587536164, 73.51538183953107, 73.51538183953107, 73.51538183953107, 73.51538183953107, 73.51538183953107, 73.51538183953107, 86.567321091526, 86.567321091526, 86.567321091526, 86.567321091526, 86.567321091526, 86.567321091526, 86.567321091526, 86.567321091526, 86.567321091526, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 99.61926034352089, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 112.67119959551582, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 125.72313884751071, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 138.77507809950563, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 151.82701735150053, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 164.87895660349545, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 177.93089585549038, 190.98283510748527, 190.98283510748527, 190.98283510748527, 190.98283510748527, 190.98283510748527, 190.98283510748527, 190.98283510748527, 190.98283510748527, 190.98283510748527, 204.0347743594802, 204.0347743594802, 204.0347743594802, 204.0347743594802, 204.0347743594802, 204.0347743594802, 204.0347743594802, 204.0347743594802, 217.08671361147512, 217.08671361147512, 217.08671361147512, 217.08671361147512, 217.08671361147512, 230.13865286347, 230.13865286347, 230.13865286347, 243.1905921154649, 243.1905921154649, 256.24253136745983, 269.2944706194547, 282.3464098714496]


def getFixArrival_150_30():
    return [70.82331459507832, 78.04881442837232, 85.27431426166633, 85.27431426166633, 92.49981409496034, 92.49981409496034, 92.49981409496034, 99.72531392825435, 99.72531392825435, 99.72531392825435, 99.72531392825435, 106.95081376154836, 106.95081376154836, 106.95081376154836, 106.95081376154836, 106.95081376154836, 106.95081376154836, 114.17631359484237, 114.17631359484237, 114.17631359484237, 114.17631359484237, 114.17631359484237, 114.17631359484237, 114.17631359484237, 114.17631359484237, 121.40181342813638, 121.40181342813638, 121.40181342813638, 121.40181342813638, 121.40181342813638, 121.40181342813638, 121.40181342813638, 121.40181342813638, 121.40181342813638, 121.40181342813638, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 128.6273132614304, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 135.8528130947244, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 143.07831292801842, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 150.30381276131243, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 157.52931259460644, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 164.75481242790045, 171.98031226119446, 171.98031226119446, 171.98031226119446, 171.98031226119446, 171.98031226119446, 171.98031226119446, 171.98031226119446, 171.98031226119446, 171.98031226119446, 179.20581209448846, 179.20581209448846, 179.20581209448846, 179.20581209448846, 179.20581209448846, 179.20581209448846, 179.20581209448846, 179.20581209448846, 186.43131192778247, 186.43131192778247, 186.43131192778247, 186.43131192778247, 186.43131192778247, 186.43131192778247, 193.65681176107648, 193.65681176107648, 193.65681176107648, 193.65681176107648, 200.8823115943705, 200.8823115943705, 200.8823115943705, 208.1078114276645, 208.1078114276645, 215.3333112609585, 222.55881109425252]


def getFixArrival_150_40():
    return [47.38268313135986, 57.943917891201636, 68.5051526510434, 68.5051526510434, 68.5051526510434,
            79.06638741088518, 79.06638741088518, 79.06638741088518, 79.06638741088518, 89.62762217072695,
            89.62762217072695, 89.62762217072695, 89.62762217072695, 89.62762217072695, 89.62762217072695,
            89.62762217072695, 100.18885693056872, 100.18885693056872, 100.18885693056872, 100.18885693056872,
            100.18885693056872, 100.18885693056872, 100.18885693056872, 100.18885693056872, 110.7500916904105,
            110.7500916904105, 110.7500916904105, 110.7500916904105, 110.7500916904105, 110.7500916904105,
            110.7500916904105, 110.7500916904105, 110.7500916904105, 110.7500916904105, 110.7500916904105,
            121.31132645025227, 121.31132645025227, 121.31132645025227, 121.31132645025227, 121.31132645025227,
            121.31132645025227, 121.31132645025227, 121.31132645025227, 121.31132645025227, 121.31132645025227,
            121.31132645025227, 121.31132645025227, 121.31132645025227, 121.31132645025227, 131.87256121009403,
            131.87256121009403, 131.87256121009403, 131.87256121009403, 131.87256121009403, 131.87256121009403,
            131.87256121009403, 131.87256121009403, 131.87256121009403, 131.87256121009403, 131.87256121009403,
            131.87256121009403, 131.87256121009403, 131.87256121009403, 131.87256121009403, 142.4337959699358,
            142.4337959699358, 142.4337959699358, 142.4337959699358, 142.4337959699358, 142.4337959699358,
            142.4337959699358, 142.4337959699358, 142.4337959699358, 142.4337959699358, 142.4337959699358,
            142.4337959699358, 142.4337959699358, 142.4337959699358, 142.4337959699358, 142.4337959699358,
            152.9950307297776, 152.9950307297776, 152.9950307297776, 152.9950307297776, 152.9950307297776,
            152.9950307297776, 152.9950307297776, 152.9950307297776, 152.9950307297776, 152.9950307297776,
            152.9950307297776, 152.9950307297776, 152.9950307297776, 152.9950307297776, 152.9950307297776,
            163.55626548961936, 163.55626548961936, 163.55626548961936, 163.55626548961936, 163.55626548961936,
            163.55626548961936, 163.55626548961936, 163.55626548961936, 163.55626548961936, 163.55626548961936,
            163.55626548961936, 163.55626548961936, 163.55626548961936, 163.55626548961936, 174.11750024946113,
            174.11750024946113, 174.11750024946113, 174.11750024946113, 174.11750024946113, 174.11750024946113,
            174.11750024946113, 174.11750024946113, 174.11750024946113, 174.11750024946113, 174.11750024946113,
            174.11750024946113, 184.6787350093029, 184.6787350093029, 184.6787350093029, 184.6787350093029,
            184.6787350093029, 184.6787350093029, 184.6787350093029, 184.6787350093029, 184.6787350093029,
            195.23996976914466, 195.23996976914466, 195.23996976914466, 195.23996976914466, 195.23996976914466,
            195.23996976914466, 195.23996976914466, 205.80120452898646, 205.80120452898646, 205.80120452898646,
            205.80120452898646, 205.80120452898646, 216.36243928882823, 216.36243928882823, 216.36243928882823,
            216.36243928882823, 226.92367404867, 226.92367404867, 237.48490880851176, 248.04614356835353]


if __name__ == "__main__":
    # ans = normalDistribution(150)
    # print(ans)
    plotArrival(getFixArrival_150_30())
    plotArrival(getFixArrival_150_40())
    plotArrival(getFixArrival_150_50())
